package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wfyn
 * @date 2023/10/6
 */

@RestController(value = "adminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "订单管理")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 根据订单编号查询订单详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/details/{id}")
    @ApiOperation("根据订单编号查询订单详情")
    public Result<OrderVO> orderDetila(@PathVariable("id") String id) throws Exception {

        OrderVO orderVO = orderService.orderDetail(id);

        return Result.success(orderVO, "查询订单详情成功");

    }

    /**
     * 拒绝订单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("拒绝订单")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        orderService.rejectionOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 订单统计
     *
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("订单统计")
    public Result<OrderStatisticsVO> statistics() {

        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO, "查询订单统计成功");
    }

    /**
     * 完成订单
     *
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    @CacheEvict(cacheNames = {"setmealCache", "dishCache"}, allEntries = true)
    public Result complete(@PathVariable String id) {
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO
     * @return
     */

    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(@ModelAttribute OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult, "订单查询成功");
    }

    /**
     * 接单
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirm(@RequestBody OrdersDTO ordersDTO) {

        orderService.confirm(ordersDTO.getId());

        return Result.success("接单成功");
    }


    /**
     * 派送
     */
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable String id) {
        orderService.delivery(id);
        return Result.success("派送成功");
    }

    /**
     * 取消订单
     *
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    @ApiOperation("拒绝订单")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        orderService.ordersCancelOrder(ordersCancelDTO);
        return Result.success();
    }
}
